package com.zz.ecommerce.fileupload;

import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 菜单dao
 *
 * @author
 */
@Repository
public interface UploadFileDao {

    @Select(
            "SELECT FILE_ID,FILE_NAME,FILE_PATH,TRUE_FILE_NAME,TO_CHAR(UPLOAD_TIME,'YYYY-MM-DD HH24:MI:SS') AS " +
                    "UPLOAD_TIME,FILE_SIZE " +
                    "FROM UPLOAD_FILE WHERE 1=1")
    List<UploadFile> queryAllFiles();

    @Select(
            "SELECT COUNT(1) " +
                    "FROM UPLOAD_FILE WHERE 1=1")
    long queryAllCount();

    @Select(
            "SELECT FILE_ID,FILE_NAME,FILE_PATH,TRUE_FILE_NAME,TO_CHAR(UPLOAD_TIME,'YYYY-MM-DD HH24:MI:SS') AS " +
                    "UPLOAD_TIME,FILE_SIZE, FILE_CONTENT " +
                    "FROM UPLOAD_FILE WHERE FILE_ID=${fileId}")
    List<UploadFile> queryFilesById(@Param("fileId") String fileId);

    @Select("SELECT to_char(SEQ_FILEID.NEXTVAL) FROM DUAL")
    String getSeqFileId();

    @Insert("INSERT INTO UPLOAD_FILE(FILE_ID,FILE_PATH,FILE_NAME,TRUE_FILE_NAME,UPLOAD_TIME,FILE_SIZE) VALUES (#{fileId}," +
            "#{filePath},#{fileName}," +
            "#{trueFileName},SYSDATE,#{fileSize})")
    long insertFile(@Param("fileId") String fileId, @Param("filePath") String filePath, @Param("fileName") String
        fileName, @Param("trueFileName") String trueFileName, @Param("fileSize") String fileSize);

    @Delete("DELETE FROM UPLOAD_FILE WHERE FILE_ID=${fileId}")
    void deleteFile(@Param("fileId") long fileId);

    @Select("SELECT FILE_ID,FILE_NAME,FILE_PATH,TRUE_FILE_NAME,TO_CHAR(UPLOAD_TIME,'YYYY-MM-DD HH24:MI:SS') AS " +
            "UPLOAD_TIME,FILE_SIZE " +
            "FROM UPLOAD_FILE WHERE FILE_ID IN(SELECT FILE_ID FROM ATTACHMENT_LIST WHERE LIST_ID=#{attachId})")
    List<UploadFile> queryAllByAttach(@Param("attachId") String attachId);

    @Update("UPDATE UPLOAD_FILE T SET T.FILE_CONTENT = #{file}\n" +
            "WHERE FILE_ID = #{fileId}")
    int updateFile(@Param("fileId") long fileId, @Param("file") byte[] file);
}
